package com.earth.amazeadmin.config.exception;

import org.apache.ibatis.javassist.NotFoundException;
import org.apache.shiro.authz.AuthorizationException;
import org.apache.shiro.authz.UnauthenticatedException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.servlet.NoHandlerFoundException;

/**
 * Created by dongjigong on 2018/5/8.
 *
 * @Email osbornjonny@126.com
 * @Description
 */
@Configuration
public class ExceptionConfig {

    Logger log = LoggerFactory.getLogger(getClass());

    @ExceptionHandler(UnauthenticatedException.class)
    @ResponseStatus(HttpStatus.UNAUTHORIZED)
    public void handleException(UnauthenticatedException e) {
        log.debug("{} was thrown", e.getClass(), e);
    }

    @ExceptionHandler(AuthorizationException.class)
    @ResponseStatus(HttpStatus.FORBIDDEN)
    public void handleException(AuthorizationException e) {
        log.debug("{} was thrown", e.getClass(), e);
    }

    @ExceptionHandler(value = {NoHandlerFoundException.class})
    @ResponseStatus(code = HttpStatus.BAD_REQUEST)
    public @ResponseBody
    ErrorMessage handleException(NotFoundException e) {
        String id = e.getMessage();
        return new ErrorMessage("Trooper Not Found: " + id + ", why aren't you at your post? " + id + ", do you copy?");
    }
}
